package com.bnyy.common.adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;

import com.bnyy.common.R;
import com.mcxtzhang.swipemenulib.SwipeMenuLayout;

public abstract class BaseSwipeDeleteAdapter<T, VH extends BaseSwipeDeleteAdapter.ViewHolder> extends BaseNormalListAdapter<T, VH> {

    public onDeleteViewClickListener<T> onDeleteViewClickListener;

    public BaseSwipeDeleteAdapter(Context context) {
        super(context);
    }

    public BaseSwipeDeleteAdapter(Context context, onDeleteViewClickListener<T> onDeleteViewClickListener) {
        super(context);
        this.onDeleteViewClickListener = onDeleteViewClickListener;
    }

    public BaseSwipeDeleteAdapter(Context context, OnItemClickListener<T> onItemViewClickListener, onDeleteViewClickListener<T> onDeleteViewClickListener) {
        super(context, onItemViewClickListener);
        this.onDeleteViewClickListener = onDeleteViewClickListener;
    }

    public interface onDeleteViewClickListener<T> extends OnItemClickListener<T> {

    }

    public abstract int getContentLayout();

    public boolean isItemHaveCorner() {
        return false;
    }

    public View createItemView(ViewGroup parent) {
        ViewGroup root = (ViewGroup) inflater.inflate(isItemHaveCorner() ? R.layout.item_base_swipe_delete_corner : R.layout.item_base_swipe_delete, parent, false);

        View item = inflater.inflate(getContentLayout(), root, false);
        ViewGroup.LayoutParams layoutParams = item.getLayoutParams();
        if (layoutParams instanceof ViewGroup.MarginLayoutParams) {
            ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) layoutParams;
            root.setPadding(marginLayoutParams.leftMargin, marginLayoutParams.topMargin, marginLayoutParams.rightMargin, marginLayoutParams.bottomMargin);
            marginLayoutParams.leftMargin = 0;
            marginLayoutParams.rightMargin = 0;
            marginLayoutParams.topMargin = 0;
            marginLayoutParams.bottomMargin = 0;
            item.setLayoutParams(marginLayoutParams);
        }

        FrameLayout fl = root.findViewById(R.id.fl_container);
        fl.addView(item);

        return root;
    }

    @Override
    public void onBindViewHolder(@NonNull VH holder, final int position) {
        super.onBindViewHolder(holder, position);
        if (onDeleteViewClickListener != null) {
            holder.delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onDeleteViewClickListener.onClick(v, getData(position), position);
                }
            });
        }
    }

    public static class ViewHolder extends BaseNormalListAdapter.ViewHolder {
        public View delete;
        public SwipeMenuLayout swipeMenuLayout;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            root = itemView.findViewById(R.id.fl_container);
            delete = itemView.findViewById(R.id.tv_delete);
            swipeMenuLayout = itemView.findViewById(R.id.swipe_menu_layout);
        }
    }
}




















